package com.xqls.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.xqls.bean.FlowRecordBean;
import com.xqls.biz.DictionaryBiz;
import com.xqls.biz.FlowRecordBiz;
import com.xqls.util.CommonPage;
import com.xqls.util.CommonResult;
import com.xqls.util.DictionaryEnums;
import com.xqls.util.GetKeyByValue;

@Controller
@RequestMapping("/flowRecord")
public class FlowRecordController {
	/**
	 * 分钟记录查询列表
	 */
	@Autowired private FlowRecordBiz flowBiz;
	@Autowired private DictionaryBiz dictionaryBiz;
	@RequestMapping("/minFlowRecordList")
	public ModelAndView getMinFlowRecordList(){
		ModelAndView view = new ModelAndView("flowRecord/minFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(  DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		view.addObject("createRecord", false);
		return view;
	}
	@RequestMapping("/minFlowRecordList/{modularNo}")
	public ModelAndView getMinFlowRecord(@PathVariable("modularNo")  Integer no){
		ModelAndView view = new ModelAndView("flowRecord/minFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent(  DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("createRecord", true);
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}
	/**
	 * 分钟记录查询数据
	 * @param commonPage
	 * @return
	 */
	@RequestMapping("/minFlowRecordData")
	@ResponseBody
	public CommonResult getMinFlowRecordListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		 CommonResult result=flowBiz.getFlowRecordByPage(commonPage,request,response);
         if(result.getIsSuccess()){
             result.setData(commonPage);
         }
		 return result;
	}
	
	
	/**
	 * 小时记录查询列表
	 */
	@RequestMapping("/hourFlowRecordList")
	public ModelAndView getHourFlowRecordList(){
		ModelAndView view = new ModelAndView("flowRecord/hourFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent( DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		view.addObject("createRecord", false);
		return view;
	}
	@RequestMapping("/hourFlowRecordList/{modularNo}")
	public ModelAndView getHourFlowRecord(@PathVariable("modularNo")  Integer no){
		ModelAndView view = new ModelAndView("flowRecord/hourFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent( DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("createRecord", true);
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}
	/**
	 * 小时记录查询数据
	 * @param commonPage
	 * @return
	 */
	@RequestMapping("/hourFlowRecordData")
	@ResponseBody
	public CommonResult getHourFlowRecordListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		 CommonResult result=flowBiz.getHourFlowRecordByPage(commonPage,request,response);
         if(result.getIsSuccess()){
             result.setData(commonPage);
         }
		 return result;
	}
	/**
	 * 生成记录
	 * @param flow
	 * @return
	 */
	@RequestMapping("/generateRecord")
	@ResponseBody
	public CommonResult generateRecord(@RequestBody FlowRecordBean flow){
		Map<String, Object> map = new HashMap<String, Object>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		map.put("enterpriseNo", flow.getEnterpriseNo());
		map.put("date", sdf.format(flow.getRecordingTime()));
		 CommonResult result=flowBiz.generateRecord(map);
		 return result;
	}
	
	/**
	 * 日记录查询列表
	 */
	@RequestMapping("/dayFlowRecordList")
	public ModelAndView getDayFlowRecordList(){
		ModelAndView view = new ModelAndView("flowRecord/dayFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent( DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		view.addObject("createRecord", false);
		return view;
	}
	@RequestMapping("/dayFlowRecordList/{modularNo}")
	public ModelAndView getDayFlowRecordList(@PathVariable("modularNo")  Integer no){
		ModelAndView view = new ModelAndView("flowRecord/dayFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent( DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("createRecord", true);
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}
	/**
	 * 日记录查询数据
	 * @param commonPage
	 * @return
	 */
	@RequestMapping("/dayFlowRecordData")
	@ResponseBody
	public CommonResult getDayFlowRecordListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		 CommonResult result=flowBiz.getDayFlowRecordByPage(commonPage,request,response);
         if(result.getIsSuccess()){
             result.setData(commonPage);
         }
		 return result;
	}
	
	/**
	 * 月统计查询列表
	 */
	@RequestMapping("/monthFlowRecordList/{modularNo}")
	public ModelAndView getMonthFlowRecordList(@PathVariable("modularNo")  Integer no){
		ModelAndView view = new ModelAndView("flowRecord/monthFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent( DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("createRecord", true);
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}
	@RequestMapping("/monthFlowRecordList")
	public ModelAndView getMonthFlowRecordList(){
		ModelAndView view = new ModelAndView("flowRecord/monthFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent( DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		view.addObject("createRecord", false);
		return view;
	}
	/**
	 * 月统计查询数据
	 * @param commonPage
	 * @return
	 */
	@RequestMapping("/monthFlowRecordData")
	@ResponseBody
	public CommonResult getMonthFlowRecordListData(@RequestBody CommonPage commonPage, HttpServletRequest request, HttpServletResponse response){
		 CommonResult result=flowBiz.getMonthFlowRecordByPage(commonPage,request,response);
         if(result.getIsSuccess()){
             result.setData(commonPage);
         }
		 return result;
	}
	/**
	 * 年统计查询列表
	 */
	@RequestMapping("/yearFlowRecordList")
	public ModelAndView getYearFlowRecordList(){
		ModelAndView view = new ModelAndView("flowRecord/yearFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent( DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		view.addObject("createRecord", false);
		return view;
	}
	@RequestMapping("/yearFlowRecordList/{modularNo}")
	public ModelAndView getYearFlowRecordList(@PathVariable("modularNo")  Integer no){
		ModelAndView view = new ModelAndView("flowRecord/yearFlowRecordList");
		Map<Integer, String> map = dictionaryBiz.queryDictionaryByParent( DictionaryEnums.COMPANY_TYPE.getCode());
		view.addObject("createRecord", true);
		view.addObject("deviceType", GetKeyByValue.getKey(map, "取水口"));
		return view;
	}
	/**
	 * 年统计查询数据
	 * @param
	 * @return
	 */
	@RequestMapping("/yearFlowRecordData")
	@ResponseBody
	public CommonResult getYearFlowRecordListData(String year,Integer block,Integer deviceType, HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("year", year);
		paramMap.put("block", block);
		paramMap.put("deviceType", deviceType);
		CommonResult result=flowBiz.getYearFlowRecordByPage(paramMap,request,response);
		return result;
	}
	
	
	@RequestMapping("/getExcelByMapForMinute/{params}")
	public void getExcelByMapForMinute(HttpServletRequest request,HttpServletResponse response,@PathVariable(value = "params") String params){	
		 JSONObject jsonObject=JSONObject.parseObject(params);
		 this.flowBiz.getMinuteFlowRecordByMap(request, response, jsonObject);
	}
	
	@RequestMapping("/getExcelByMapForHour/{params}")
	public void getExcelByMapForHour(HttpServletRequest request,HttpServletResponse response,@PathVariable(value = "params") String params){	
		 JSONObject jsonObject=JSONObject.parseObject(params);
		 this.flowBiz.getHourFlowRecordByMap(request, response, jsonObject);
	}
	
	@RequestMapping("/getExcelByMapForDay/{params}")
	public void getExcelByMapForDay(HttpServletRequest request,HttpServletResponse response,@PathVariable(value = "params") String params){	
		 JSONObject jsonObject=JSONObject.parseObject(params);
		 this.flowBiz.getDayFlowRecordByMap(request,response,jsonObject);
	}
	
	@RequestMapping("/getExcelByMapForMonth/{params}")
	public void getExcelByMapForMonth(HttpServletRequest request,HttpServletResponse response,@PathVariable(value = "params") String params){	
		 JSONObject jsonObject=JSONObject.parseObject(params);
		 this.flowBiz.getMonthFlowRecordByMap(request, response, jsonObject);
	}
	
	@RequestMapping("/getExcelByMapForYear/{params}")
	public void getExcelByMapForYear(HttpServletRequest request,HttpServletResponse response,@PathVariable(value = "params") String params){	
		 JSONObject jsonObject=JSONObject.parseObject(params);
		 this.flowBiz.getYearFlowRecordByMap(request, response, jsonObject);
	}
}
